package indi.kider.server.bill.api;


import indi.kider.common.model.bill.BillPrepaidInfo;
import indi.kider.server.bill.model.CommRespRegitAndDeductModel;
import indi.kider.server.bill.model.MeterAnbsValue;
import indi.kider.server.bill.service.PrepaidBillService;
import indi.kider.server.bill.service.ProduceBillService;
import indi.kider.server.bill.service.RentInfoEveryDay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;

import java.util.List;

@RestController
@RequestMapping("produce")
@Slf4j
public class ProduceBillController {

    @Autowired
    private ProduceBillService produceBillService;

    @Autowired
    private PrepaidBillService prepaidBillService;

    @Autowired
    private RentInfoEveryDay rentInfoEveryDay;

    /**
     * 账户充值
     *
     * @return
     */
    @PostMapping("/payOnlineAccount")
    public CommonResponse payOnlineAccount() {
        return null;
    }

//    /**
//     * 账单缴费
//     *
//     * @return
//     */
//    @PostMapping("/payOnlineAccount")
//    public CommonResponse payOnlineBill(){
//        return null;
//    }

    /**
     * 执行时间的当月的后付费项目的账单。
     *
     * @return
     */
    @PostMapping("/produceThisMonthBill")
    public CommonResponse produceThisMonthBill() {
        try {
            log.info("【开始结算用户本月的账单】");
            produceBillService.produceAllConsumeBill();
            //produceBillService.produceAllRentBill();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 退租是时候使用
     *
     * @param meterAnbsValue
     * @return
     */
    @PostMapping("/produceThisMonthBillByRent")
    public CommonResponse produceThisMonthBillByRent(@RequestBody MeterAnbsValue meterAnbsValue) {
        try {
            log.info("【开始结算{}本月的账单】", meterAnbsValue);
            int tr = produceBillService.settlementBill(meterAnbsValue);
            if (tr == 2)
                return CommonResponse.buildBadRequestResponse("获取读数失败");
            if (tr == 1)
                return CommonResponse.buildBadRequestResponse("结算失败");
            if (tr == 3)
                return CommonResponse.buildBadRequestResponse("预付费账户余额不足");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
        return CommonResponse.buildAddSuccessResponse("");
    }

    /**
     * 月份的后付费相关项目的账单。
     *
     * @param month
     * @return
     */
    @PostMapping("/produceMonthBill")
    public CommonResponse produceMonthBill(String month) {
        try {
            log.info("【开始结算{}的用户账单】", month);
            produceBillService.produceAllConsumeBill(month);
            //produceBillService.produceAllRentBill();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 每天入住的房租账单
     *
     * @return
     */
    @PostMapping("/produceRentBill")
    public CommonResponse produceRentBill() {
        try {
            produceBillService.produceAllRentBill();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @DeleteMapping("/")
    public CommonResponse delBill(String month) {
        try {
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }


    @PostMapping("/extractRentInfo")
    public CommonResponse extractRentInfo() {
        try {
            rentInfoEveryDay.extractRentInfo();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("/produceEveryDayBill")
    public CommonResponse produceEveryDayBill() {
        try {
            log.info("【开始执行每日的账单任务】");
            prepaidBillService.produceBillEveryDay();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("/settlementBillEveryDay")
    public CommonResponse settlementBillEveryDay() {
        try {
            log.info("【开始每日的账单进行扣款】");
            prepaidBillService.settlementBillEveryDay();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 创建钱包
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/regitWallet/{tenantId}")
    public CommonResponse<CommRespRegitAndDeductModel> regitWallet(@PathVariable Long tenantId) {
        try {
            CommRespRegitAndDeductModel commRespRegitAndDeductModel = prepaidBillService.regitWallet(tenantId);
            return CommonResponse.buildAddSuccessResponse(commRespRegitAndDeductModel);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 创建钱包自动定时
     *
     * @param
     * @return
     */
    @PostMapping("/regitWallet")
    public CommonResponse regitWallet() {
        try {
            prepaidBillService.regitWallet();
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 退房钱包扣款。
     *
     * @param rentId
     * @return
     */
    @PostMapping("/deductionBill/{rentId}")
    public CommonResponse<List<CommRespRegitAndDeductModel>> deductionBill(@PathVariable Long rentId) {
        try {
            log.info("【退房钱包扣款】");
            List<CommRespRegitAndDeductModel> list = prepaidBillService.deductionBill(rentId);
            return CommonResponse.buildAddSuccessResponse(list);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 按月份查询扣款的明细。
     *
     * @param billPrepaidInfo
     * @return
     */
    @PostMapping("/selectPrepaidInfoByRentId")
    public CommonResponse<List<BillPrepaidInfo>> selectPrepaidInfoByRentId(@RequestBody BillPrepaidInfo billPrepaidInfo) {
        try {
            log.info("【按月份查询扣款的明细】");
            Long                  rentId = billPrepaidInfo.getBillAttribution();
            String                month  = billPrepaidInfo.getBillDate();
            List<BillPrepaidInfo> list   = prepaidBillService.selectPrepaidInfoByRentId(rentId, month);
            return CommonResponse.buildGetSuccessResponse(list);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 按用户编号查询是否余额不足。
     *
     * @param tenantId
     * @return
     */
    @PostMapping("/judgingTheBalanceByTenantId/{tenantId}")
    public CommonResponse judgingTheBalanceByTenantId(@PathVariable Long tenantId) {
        try {
            log.info("【按用户编号{}查询是否余额不足】",tenantId);
            boolean judging = prepaidBillService.judgingTheBalanceByTenantId(tenantId);
            return CommonResponse.buildGetSuccessResponse(judging);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 根据用户ID 判断是否可以退押金。
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/canRefund/{tenantId}")
    public CommonResponse canRefund(@PathVariable Long tenantId) {
        try {
            log.info("【查看{}是否已经退租】",tenantId);
            boolean canrefun = prepaidBillService.canRefund(tenantId);
            return CommonResponse.buildGetSuccessResponse(canrefun);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

//    @PostMapping("/testApi")
//    public CommonResponse testApi() {
//        try {
//            prepaidBillService.regitWallet(2354l);
//            prepaidBillService.summaryMonthBill("cc");
//            return CommonResponse.buildAddSuccessResponse("");
//        } catch (Exception e) {
//            return CommonResponse.buildExceptionResponse(e);
//        }
//    }

}
